<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>展示订单列表</title>
    <link rel="stylesheet" type="text/css" href="../../../resources/jquery-easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../../resources/jquery-easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../../../resources/jquery-easyui/demo/demo.css">
    <link rel="stylesheet" type="text/css" href="../../../resources/login/css/demo.css">
    <!--<%&#45;&#45;<link rel="stylesheet" type="text/css" href="lib/jquery-easyui/demo.css">&#45;&#45;%>-->
    <script type="text/javascript" src="../../../resources/jquery-easyui/jquery.min.js"></script>
    <script type="text/javascript" src="../../../resources/jquery-easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../../resources/admin/urlAdmin.js"></script>
</head>
<body>
<table id="dg" data-options="rownumbers:true,
singleSelect:true,
autoRowHeight:false,
pagination:true">
<div id="toolbar">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" id="addUser">添加用户</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" id="editUser" onclick="editUser()">编辑用户</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除用户</a>
</div>
    <!--<div id="toolBarbtn">-->
        <!-- -->
    <!--</div>-->
    <div id="myDlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" id="myDlg-saveBtn">确定</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" id="myDlg-closeBtn">取消</a>
    </div>

    <div id="myDlg" class="easyui-dialog" title="修改用户信息" style="width:400px;height:200px;padding:10px">
        <div style="padding:3px 2px;border-bottom:1px solid #ccc">Ajax Form</div>
        <form id="ff" method="post">
            <div>
                <label >账号:</label>
                <input class="" type="text" id="account" name="account" data-options="required:true" />
                <input class="" type="hidden" id="id" name="id"/>
            </div>
            <div>
                <label >手机：</label>
                <input class="" id="mobile" type="text" name="mobile" />
            </div>
            <div>
                <label >角色：</label>
                <input id="qysjzt" name="roleId" class="easyui-combobox" width="50px">
                <input style="display: none" type="submit"/>
            </div>

        </form>
    </div>
</table>

</body>
<script>
    function save(){
        alert("save")
    }
    function show() {
//
        var data=[];
        $.ajax({
            type: "get",
            url: netpath+"/operator/getAllUsers",
            timeout: 3000,
            success: function (result) {
                data=result.data;
                for(let i=0;i<data.length;i++){
                    data[i].name=data[i].role.name;
                    data[i].role_id=data[i].role.id;
                }
                $('#dg').datagrid('loadData', data);
            },
            error: function (request) {
               alert(request.error)
            }});


        return data
    }

    function fLoadTable() {
        $('#dg').datagrid({
            title: '用户列表',
            width: 900,
            height: 500,
            buttons:'#toolbar',
            fitColumns: true,

//对应json数据中的每一列
            columns: [[ {
                field: 'id',
                width: '100',
                title: '用户id',
                align: 'center',
                checkbox:true
            }, {
                field: 'account',
                title: '用户账号',
                width: '100',
                align: 'center'
            }, {
                field: 'mobile',
                title: '手机号',
                width: '100',
                align: 'center',
            }, {
                field: 'name',
                title: '角色',
                width: '100',
                align: 'center',
            }, {
                field: 'role_id',
                title: '角色id',
                width: '100',
                align: 'center',
//                hidden:'false'
            }
            ]],
            idField: 'id',
            loadMsg: 'Processing, please wait …',
            pagination: true
        });
    }
    $(document).ready(function () {
        fLoadTable();
        show();
        $('#cc').combobox({
//            url:netpath+"/operator/selectRoles",
//            valueField:'id',
//            textField:'text'
//            onLoadSuccess : function(){
//                $('#cc').combobox('selectIndex',1);
//            },
        });

        /**
         * 对话框
          */
openDialog();


    });
    function openDialog(){
        //设置一个全局变量来控制弹出框关闭
        var bClose=false;
        //弹出框的属性设置
        $("#myDlg").dialog({
            modal: true,
            closable: true,
            closed: true,
            left: 150,
            top:50,
//            href: '/operator/getOneUserById?id=' + rowIndex,
            buttons:'#myDlg-buttons',
            onBeforeClose:function(){//关闭弹出框之前动作-弹出提示
                if (!bClose) {
//                    $.messager.confirm("提醒", "确定关闭该窗口?",function (r) {
//                        if (r) {
//                            bClose = true;//标记可以退出
//
//                        }
//                    });
                    bClose = true;//标记可以退出
                }
                return bClose;  //通过全局变量来控制是否关闭窗口
            }
        });
        //弹出框打开,修改用户
        $('#editUser').click(function(){
            //先设置为false;
            bClose=false;
            getID();
            $('#myDlg').dialog("open");
            selectRoles();
        });
 //弹出框打开，新增用户
        $('#addUser').click(function(){
            //先设置为false;
            bClose=false;
            $('#myDlg').dialog("open");
            selectRoles();
        });



        //点击保存
        $('#myDlg-saveBtn').click(function(){

            $('#ff').form('submit',{
                url: netpath+"/operator/addDrEditUser",
                method:"post",
                onSubmit: function (operator) {
//
                },
                success: function (data) {
                    var dat = $.parseJSON(data);
                    if (dat.success) {
                        $.messager.alert("提示信息",dat.data,"info");
                        setTimeout(function(){
                            window.location.reload();
                        },1000)
                    }
                    else{
                        alert(dat.error)
                    }
                }
            });
//            $('#ff').submit();
            //这里在后面加true,将会绕过onBeforeClose事件,弹出框直接关闭
            $('#myDlg').dialog("close",true);
        });

        //点击关闭
        $('#myDlg-closeBtn').click(function(){
            $('#myDlg').dialog("close");
        });

        //得到选中的 ID
        getID=function(){
            var row=$('#dg').datagrid('getSelected');
            if (row){
                alert('Item ID:'+row.id+'mobile:'+row.mobile);
            }
            $("#mobile").val(row.mobile);
            $("#account").val(row.account);
            $("#id").val(row.id);
//            $("#cc").combobox('select',row.name);
            return row;
        }
    }
    function selectRoles(){
        $.ajax({
            url:netpath+"/operator/selectRoles",
            dataType:"json",
            type:"GET",
            data:{
                "type":"audit_state"
            },
            success:function(data){
                //绑定第一个下拉框
                $('#qysjzt').combobox({
                            data: data.data,
                            valueField: 'id',
                            textField: 'name'}
                );
            },
            error:function(data){
                alert("初始化下拉控件失败");
            }
        })
    }
</script>
</html>